{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7dbeba6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "aca11135",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "2.6.0+cpu\n"
     ]
    }
   ],
   "source": [
    "print(torch.cuda.is_available())\n",
    "print(torch.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "6f9aa832",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[1, 2],\n",
       "         [3, 4]]),\n",
       " tensor([[1, 2],\n",
       "         [3, 4]]),\n",
       " tensor([[1, 2],\n",
       "         [3, 4]]))"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "W = torch.tensor([[1,2],[3,4]])\n",
    "X = torch.tensor([[1,2],[3,4]])\n",
    "Y = torch.tensor([[1,2],[3,4]])\n",
    "W, X, Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c6698b0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[-3.7160e+27,  8.3798e-43,  1.8750e+00],\n",
       "         [ 1.8750e+00,  1.8750e+00,  0.0000e+00]]),\n",
       " tensor([[0., 0., 0.],\n",
       "         [0., 0., 0.]]),\n",
       " tensor([[1., 1., 1.],\n",
       "         [1., 1., 1.]]))"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w = torch.empty(2,3) # 创建一个2行3列的矩阵，元素值随机\n",
    "x = torch.zeros(2,3) # 创建一个2行3列的矩阵，元素值为0\n",
    "y = torch.ones(2,3) # 创建一个2行3列的矩阵，元素值为1\n",
    "w, x, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "8b721c6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.4675, 0.2468, 0.1525],\n",
      "        [0.4027, 0.5047, 0.5172],\n",
      "        [0.7172, 0.5239, 0.6089]])\n",
      "tensor([[-0.1629,  0.1029,  0.7095],\n",
      "        [-0.2023, -1.9241,  0.6671],\n",
      "        [-0.7045, -1.5642,  1.9029]])\n",
      "tensor([[3, 5, 4],\n",
      "        [6, 1, 9],\n",
      "        [8, 8, 3]])\n"
     ]
    }
   ],
   "source": [
    "w = torch.rand(3,3) # 创建一个3行3列的矩阵，元素值随机\n",
    "print(w)\n",
    "w = torch.randn(3,3) # 创建一个3行3列的矩阵，元素值随机，服从标准正态分布\n",
    "print(w)\n",
    "w = torch.randint(1,10,(3,3)) # 创建一个3行3列的矩阵，元素值随机，范围1-10\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "b8d9b613",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[4.9407e-324,  0.0000e+00,  0.0000e+00],\n",
      "        [ 0.0000e+00,  0.0000e+00,  0.0000e+00],\n",
      "        [ 0.0000e+00,  0.0000e+00,  0.0000e+00]], dtype=torch.float64)\n"
     ]
    }
   ],
   "source": [
    "w = torch.empty(3,3, dtype=torch.float64, device='cpu') # 创建一个3行3列的矩阵，元素值随机 // dtype指定数据类型 device指定设备\n",
    "print(w)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d65f6893",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.2709e-311,  0.0000e+00,  0.0000e+00],\n",
      "        [ 0.0000e+00,  0.0000e+00,  0.0000e+00],\n",
      "        [ 0.0000e+00,  0.0000e+00,  0.0000e+00],\n",
      "        [ 0.0000e+00,  0.0000e+00,  0.0000e+00],\n",
      "        [ 0.0000e+00,  0.0000e+00,  0.0000e+00]], dtype=torch.float64,\n",
      "       requires_grad=True)\n"
     ]
    }
   ],
   "source": [
    "x = torch.empty(5, 3, dtype=torch.float64, device=torch.device('cpu'), requires_grad=True) # 创建一个5行3列的矩阵，元素值随机 // dtype指定数据类型 device指定设备 requires_grad指定是否需要计算梯度\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "e5670aa0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.2709e-311, 4.9407e-324, 4.9407e-324],\n",
      "        [4.9407e-324, 4.9407e-324, 4.9407e-324],\n",
      "        [4.9407e-324, 4.9407e-324, 4.9407e-324]], dtype=torch.float64)\n"
     ]
    }
   ],
   "source": [
    "x = torch.empty_like(w) # 创建一个和w相同形状的tensor, 但数据未初始化，相当于copy但是没有黏贴\n",
    "print(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caac5832",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
